﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.DAL;
using MyDataService;

namespace QuanLyBanHang
{
    public partial class frmNhapHang : Form
    {
        public frmNhapHang()
        {
            InitializeComponent();
            dataservice.strConn = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;

        }

        private bool FormLoad = true;
        DalBanHang banHang=new DalBanHang();
        DalNhapHang nhapHang=new DalNhapHang();

        private void frmNhapHang_Load(object sender, EventArgs e)
        {
            banHang.loadcboHangHoa(cboDSHH);
            FormLoad = false;
        }

        private void cboDSHH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (FormLoad == true)
            {
                return;
            }
            lblMaHangHoa.Text = cboDSHH.SelectedValue.ToString();
        }

        private void cboDSHH_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (cboDSHH.Text=="")
                {
                    lblMaHangHoa.Text = "";
                }
                
            }
            catch (Exception)
            {
                
            }
            
        }

        private void lblMaHangHoa_TextChanged(object sender, EventArgs e)
        {
            if (lblMaHangHoa.Text=="")
            {
                btnNhapVao.Image = Properties.Resources.add;
                btnNhapVao.Text = "THÊM HÀNG MỚI";
            }
            else
            {
                if (nhapHang.exMaVach(lblMaHangHoa.Text))
                {
                    btnNhapVao.Image = Properties.Resources.Package_add;
                    btnNhapVao.Text = "NHẬP VÀO";
                }
            }
        }

        private void btnNhapVao_Click(object sender, EventArgs e)
        {
            if (btnNhapVao.Text == "NHẬP VÀO")
            {
                
                if (nhapHang.NhapHangHoa(lblMaHangHoa.Text,int.Parse(nudSoLuong.Value.ToString()),txtGiaTien.Text))
                {
                    MessageBox.Show("Nhập hàng thành công");
                    cboDSHH.Text = "";
                    lblMaHangHoa.Text = "";
                    txtGiaTien.Text = "";
                    nudSoLuong.Value = 1;
                }
            }
        }
    }
}
